<template>
  <div class="sc-chat-window" :class="{opened: isOpen, closed: !isOpen}">
    <Header
      v-if="showHeader"
      :title="title"
      :colors="colors"
      @close="$emit('close')"
      @userList="handleUserListToggle"
    >
      <template>
        <slot name="header"></slot>
      </template>
    </Header>
    <UserList v-if="showUserList" :colors="colors" :participants="participants" />
    <MescrollVue ref="mescroll" :down="mescrollDown" :up="mescrollUp" @init="mescrollInit">
      <MessageList
        v-if="!showUserList"
        ref="msgList"
        :messages="messages"
        :participants="participants"
        :show-typing-indicator="showTypingIndicator"
        :colors="colors"
        :always-scroll-to-bottom="alwaysScrollToBottom"
        :message-styling="messageStyling"
        @scrollToTop="$emit('scrollToTop')"
        @remove="$emit('remove', $event)"
      >
        <template v-slot:user-avatar="scopedProps">
          <slot name="user-avatar" :user="scopedProps.user" :message="scopedProps.message"></slot>
        </template>
        <template v-slot:text-message-body="scopedProps">
          <slot
            name="text-message-body"
            :message="scopedProps.message"
            :messageText="scopedProps.messageText"
            :messageColors="scopedProps.messageColors"
            :me="scopedProps.me"
          >
          </slot>
        </template>
        <template v-slot:system-message-body="scopedProps">
          <slot name="system-message-body" :message="scopedProps.message"></slot>
        </template>
        <template v-slot:text-message-toolbox="scopedProps">
          <slot name="text-message-toolbox" :message="scopedProps.message" :me="scopedProps.me">
          </slot>
        </template>
      </MessageList>
    </MescrollVue>
    <UserInput
      v-if="!showUserList"
      :show-emoji="showEmoji"
      :on-submit="handleInputSubmit"
      :suggestions="getSuggestions()"
      :show-file="showFile"
      :placeholder="placeholder"
      :colors="colors"
      @onType="$emit('onType')"
      @edit="$emit('edit', $event)"
    />
  </div>
</template>

<script>
import Header from './Header.vue'
import MessageList from './MessageList.vue'
import UserInput from './UserInput.vue'
import UserList from './UserList.vue'
import MescrollVue from 'mescroll.js/mescroll.vue'

export default {
  components: {
    Header,
    MessageList,
    UserInput,
    UserList,
    MescrollVue
  },
  props: {
    showEmoji: {
      type: Boolean,
      default: false
    },
    showFile: {
      type: Boolean,
      default: false
    },
    autoInit: {
      type: Boolean,
      default: false
    },
    showHeader: {
      type: Boolean,
      default: true
    },
    participants: {
      type: Array,
      required: true
    },
    title: {
      type: String,
      required: true
    },
    onUserInputSubmit: {
      type: Function,
      required: true
    },
    messageList: {
      type: Array,
      default: () => []
    },
    isOpen: {
      type: Boolean,
      default: () => false
    },
    placeholder: {
      type: String,
      required: true
    },
    showTypingIndicator: {
      type: String,
      required: true
    },
    colors: {
      type: Object,
      required: true
    },
    alwaysScrollToBottom: {
      type: Boolean,
      required: true
    },
    messageStyling: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      showUserList: false,
      mescroll: null, // mescroll实例对象
      mescrollDown: {
        auto: this.autoInit, // 是否在初始化完毕之后自动执行一次下拉刷新的回调 callback
        callback: this.downCallback,
        textInOffset: '下拉刷新', // 下拉刷新
        textOutOffset: '释放即可刷新...', // 释放更新
        textLoading: '加载中...' // 加载中
      }, //下拉刷新的配置. (如果下拉刷新和上拉加载处理的逻辑是一样的,则mescrollDown可不用写了)
      mescrollUp: {
        use: false
      }
    }
  },
  computed: {
    messages() {
      let messages = this.messageList

      return messages
    }
  },
  beforeDestroy() {
    if (this.mescroll) {
      this.mescroll.destroy()
      this.mescroll = null
    }
  },
  methods: {
    downCallback(mescroll) {
      this.$emit('down-callback', mescroll)
    },
    // mescroll组件初始化的回调,可获取到mescroll对象
    mescrollInit(mescroll) {
      this.mescroll = mescroll // 如果this.mescroll对象没有使用到,则mescrollInit可以不用配置
      if (this.alwaysScrollToBottom) {
        this.mescroll.scrollTo(99999, 0)
      }
    },
    handleUserListToggle(showUserList) {
      this.showUserList = showUserList
    },
    getSuggestions() {
      return this.messages.length > 0 ? this.messages[this.messages.length - 1].suggestions : []
    },
    handleInputSubmit(message) {
      this.$refs.msgList.onAddMessage()
      this.onUserInputSubmit(message)
    }
  }
}
</script>

<style scoped>
.sc-chat-window {
  width: 370px;
  height: calc(100% - 120px);
  max-height: 590px;
  position: fixed;
  right: 25px;
  bottom: 20px;
  box-sizing: border-box;
  box-shadow: 0px 7px 40px 2px rgba(148, 149, 150, 0.1);
  background: white;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-radius: 10px;
  font-family: -apple-system, system-ui, BlinkMacSystemFont, 'Segoe UI', Roboto, Ubuntu,
    'Helvetica Neue', Arial, sans-serif;
  animation: fadeIn;
  animation-duration: 0.3s;
  animation-timing-function: ease-in-out;
}

.sc-chat-window.closed {
  opacity: 0;
  display: none;
  bottom: 90px;
}

@keyframes fadeIn {
  0% {
    display: none;
    opacity: 0;
  }

  100% {
    display: flex;
    opacity: 1;
  }
}

.sc-message--me {
  text-align: right;
}

.sc-message--them {
  text-align: left;
}

.sc-chat-window {
  width: 100%;
  height: 100%;
  max-height: 100%;
  right: 0px;
  bottom: 0px;
  border-radius: 0px;
}

.sc-chat-window {
  transition: 0.1s ease-in-out;
}

.sc-chat-window.closed {
  bottom: 0px;
}

/*@media (max-width: 450px) {
  .sc-chat-window {
    width: 100%;
    height: 100%;
    max-height: 100%;
    right: 0px;
    bottom: 0px;
    border-radius: 0px;
  }
  .sc-chat-window {
    transition: 0.1s ease-in-out;
  }
  .sc-chat-window.closed {
    bottom: 0px;
  }
}*/
</style>
